#include "uart/uart_api.h"


#if (DEVICE_DEBUG > DEVICE_DEBUG_OFF)
static const char *TAG = "UART_API";
#endif

DEVICE_PUBLIC(uart_device_t *) uart_device_find(const char *name) {
    if (!name) {
        DEVICE_LOG_ERROR(TAG, "uart_device_find error");
        return NULL;
    }
    return device_find(name);
}

DEVICE_PUBLIC(int) uart_device_init(uart_device_t *dev, uart_device_config_t *cfg) {
    if (!dev || !cfg) {
        DEVICE_LOG_ERROR(TAG, "uart_device_init param error");
        return -1;
    }
    return device_open(dev, cfg);
}

DEVICE_PUBLIC(int) uart_device_deinit(uart_device_t *dev) {
    if (!dev) {
        DEVICE_LOG_ERROR(TAG, "uart_device_deinit param error");
        return -1;
    }
    return device_close(dev);
}


DEVICE_PUBLIC(int) uart_device_received(uart_device_t *dev, void *data, uint32_t len) {
    if (!dev || !data) {
        DEVICE_LOG_ERROR(TAG, "uart_device_received param error");
        return -1;
    }
    return device_read(dev, data, len);
}

DEVICE_PUBLIC(int) uart_device_transmit(uart_device_t *dev, void *data, uint32_t len) {
    if (!dev || !data) {
        DEVICE_LOG_ERROR(TAG, "uart_device_transmit param error");
        return -1;
    }
    return device_write(dev, data, len);
}

DEVICE_PUBLIC(int) uart_device_set_baudrate(uart_device_t *dev, uart_device_baudrate_t baudrate) {
    if (!dev) {
        DEVICE_LOG_ERROR(TAG, "uart_device_set_baudrate param error");
        return -1;
    }
    return device_ioctl(dev, UART_CTRL_CMD_SET_BAUDRATE, baudrate);
}

DEVICE_PUBLIC(int) uart_device_set_databits(uart_device_t *dev, uart_device_data_bits_t databits) {
    if (!dev) {
        DEVICE_LOG_ERROR(TAG, "uart_device_set_databits param error");
        return -1;
    }
    return device_ioctl(dev, UART_CTRL_CMD_SET_DATABITS, databits);
}
DEVICE_PUBLIC(int) uart_device_set_stopbits(uart_device_t *dev, uart_device_stop_bits_t stopbits) {
    if (!dev) {
        DEVICE_LOG_ERROR(TAG, "uart_device_set_stopbits param error");
        return -1;
    }
    return device_ioctl(dev, UART_CTRL_CMD_SET_STOPBITS, stopbits);
}

DEVICE_PUBLIC(int) uart_device_set_parity(uart_device_t *dev, uart_device_parity_t parity) {
    if (!dev) {
        DEVICE_LOG_ERROR(TAG, "uart_device_set_parity param error");
        return -1;
    }
    return device_ioctl(dev, UART_CTRL_CMD_SET_PARITY, parity);
}

DEVICE_PUBLIC(int) uart_device_set_flowctrl(uart_device_t *dev, uart_device_flow_control_t flowctrl) {
    if (!dev) {
        DEVICE_LOG_ERROR(TAG, "uart_device_set_flowctrl param error");
        return -1;
    }
    return device_ioctl(dev, UART_CTRL_CMD_FLOWCTRL, flowctrl);
}

DEVICE_PUBLIC(int) uart_device_event_cb_add(uart_device_t *dev, uart_event_cb evt_cb, void *user_data) {
    if (!dev) {
        DEVICE_LOG_ERROR(TAG, "uart_device_event_cb_add param error");
        return -1;
    }
    return device_ioctl(dev, UART_CTRL_CMD_SET_EVENT_CB_ADD, evt_cb, user_data);
}

DEVICE_PUBLIC(int) uart_device_event_cb_remove(uart_device_t *dev) {
    if (!dev) {
        DEVICE_LOG_ERROR(TAG, "uart_device_event_cb_remove param error");
        return -1;
    }
    return device_ioctl(dev, UART_CTRL_CMD_SET_EVENT_CB_REMOVE);
}

DEVICE_PUBLIC(int) uart_device_irq_enable(uart_device_t *dev) {
    if (!dev) {
        DEVICE_LOG_ERROR(TAG, "uart_device_irq_enable param error");
        return -1;
    }
    return device_ioctl(dev, UART_CTRL_CMD_SET_ISR_ENABLE);
}
DEVICE_PUBLIC(int) uart_device_irq_disable(uart_device_t *dev) {
    if (!dev) {
        DEVICE_LOG_ERROR(TAG, "uart_device_irq_disable param error");
        return -1;
    }
    return device_ioctl(dev, UART_CTRL_CMD_SET_ISR_DISABLE);
}

EXTERN DEVICE_PUBLIC(int) uart_device_lowpower_enter(uart_device_t *dev) {
    if (!dev) {
        DEVICE_LOG_ERROR(TAG, "uart_device_lowpower_enter param error");
        return -1;
    }
    return device_ioctl(dev, UART_CTRL_CMD_SET_LOWPOWER_ENTER);
}
EXTERN DEVICE_PUBLIC(int) uart_device_lowpower_exit(uart_device_t *dev) {
    if (!dev) {
        DEVICE_LOG_ERROR(TAG, "uart_device_lowpower_exit param error");
        return -1;
    }
    return device_ioctl(dev, UART_CTRL_CMD_SET_LOWPOWER_EXIT);
}

